import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const routerContext = require.context('./modules', true, /\.js$/) // 获取路由模块的上下文
let indexRouter = {
    // 默认页面
    path: '/',
    component: () => import('@/view/index'),
    redirect: '/index',
    children: []
  }

  let routes = [
      
      indexRouter,
      {
        path: '*',
        name: '404',
      component: ()=> import('@/view/error/nofound'),
      meta: {
        index: 1
      }
    }
  ]

// 获得路由的属性
routerContext.keys().forEach(route => {
   const routerModule =  routerContext(route) // 得到路由的模块
   
   // 兼容 import export 和 require module.export 两种规范
   
  indexRouter.children = [...indexRouter.children, ...(routerModule.default || routerModule)]
});

let router = new Router({
    routes: routes
})
router.beforeEach((to, from,next) => {
  document.title = to.meta.title
  next()
})

export default router

/* import Vue from "vue"
import Router from "vue-router"
Vue.use(Router)

const routes = [{
  path: "/",
  redirect: "/index",

  meta: {
      title: "首页",
  }
},
{
  path: "/index",

  component: () =>

    import ("@/view/home/index"),
  },
  {
    path: "/mine",
  
    component: () =>
  
      import ("@/view/mine/index"),
    },
]

let router = new Router({
  routes
})

export default router; */